/*
 * defs.h
 *
 *  Created on: 2012-3-3
 *      Author: lin
 */

#ifndef DEFS_H_
#define DEFS_H_

#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <assert.h>
#include <stdlio.h>
#include <unistd.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/types.h>;
#include <sys/stat.h>

#ifdef  __cplusplus
# define __BEGIN_DECLS  extern "C" {
# define __END_DECLS    }
#else
# define __BEGIN_DECLS
# define __END_DECLS
#endif

#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
# define inline
# define register
# if defined __GNUC__ && __GNUC__ >= 2
#  define __func__ __FUNCTION__
# else
#  define __func__ "<unknown>"
# endif
#endif

#define NOTUSED(V) ((void) V)

#define STR_NEWLINE "\n"
#define STR_NEWLINE_LEN 1

#define STR_INT32_LEN   sizeof("-2147483648") - 1
#define STR_INT64_LEN   sizeof("-9223372036854775808") - 1

typedef union {
    void* ptr;
    uint64_t u64;
    int64_t s64;
} mixed_t;

typedef uint32_t (*mixed_t_hash_fn)(const mixed_t key);
typedef int (*mixed_t_dup_fn)(const mixed_t val, mixed_t* copy);
typedef void (*mixed_t_free_fn)(const mixed_t val);
typedef int (*mixed_t_match_fn)(const mixed_t val, const mixed_t match);
typedef int (*mixed_t_compare_fn)(const mixed_t l, const mixed_t r);

static inline int mixed_t_cmpare_string(const mixed_t l, const mixed_t r) {
    return strcmp(l.ptr, r.ptr);
}
#define mixed_t_free(free_fn,mix)\
    do{\
        mixed_t_free_fn _free_fn = (free_fn);\
        if(_free_fn){\
            _free_fn(mix);\
        }\
    }while(0)

typedef enum direction {
    D_OBVERSE, D_REVERSE
} direction_t;

#define fatal_no(label,no) \
    do{\
        const char *_label=(label);\
        const int _errno = (no);\
        char buf[64]={'\0',};\
        char *err = strerror_r(_errno, buf, 64);\
        fprintf(stderr, "fatal cause abort[%s:%d:%s][%d,%s], %s \n",__FILE__,__LINE__,__func__, _errno,err,_label);\
        abort();\
    }while(0)

#define errors_no(label,no) \
    do{\
        const char *_label=(label);\
        const int _errno = (no);\
        char buf[64]={'\0',};\
        char *err = strerror_r(_errno, buf, 64);\
        fprintf(stderr, "error occured[%s:%d:%s][%d,%s], %s \n",__FILE__,__LINE__,__func__, _errno,err,_label);\
    }while(0)

#define errors(label) \
    do{\
        const char *_label=(label);\
        fprintf(stderr, "error occuredt[%s:%d:%s], %s \n",__FILE__,__LINE__,__func__,_label);\
    }while(0)
#endif /* DEFS_H_ */
